package com.manager.gen.module.service.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.commons.mybatis.service.GenericService;
import com.manager.commons.utils.PinyinUtil;
import com.manager.commons.utils.StringUtil;
import com.manager.commons.utils.UUIDUtil;
import com.manager.core.web.json.AjaxJson;
import com.manager.gen.module.dao.file.GenFileUserDao;
import com.manager.gen.module.dao.user.GenUserDao;
import com.manager.gen.module.entity.TransferData;
import com.manager.gen.module.entity.file.GenFileUser;
import com.manager.gen.module.entity.user.GenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * GenUserService
 * 用户表
 *
 * @author manager
 * @version 1.0
 */
@Service
public class GenUserService extends GenericService<GenUser> {

    @Autowired
    private GenUserDao jdbcDao;
    @Autowired
    private GenFileUserDao fileUserDao;

    @Override
    public GenericDao<GenUser> getDao() {
        return jdbcDao;
    }

    public String getQueryUser() {
        List<TransferData> list = jdbcDao.queryUser();
        return JSON.toJSONString(list);
    }

    /**
     * 以选择用户
     * @param map
     * @return
     */
    public String getCheckUser(Map<String, Object> map) {
        List<String> list = fileUserDao.queryFileUser(map);
        return JSON.toJSONString(list);
    }

    /**
     * 保存
     *
     * @param params
     * @return
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public AjaxJson mySave(Map<String, Object> params) throws Exception {
        if (null != params) {
            String _id = String.valueOf(params.get("_id"));
            String name = String.valueOf(params.get("name"));
            String py = PinyinUtil.toPinyin(name);
            int f = 0;
            params.put("pinyin", py);
            if (StringUtil.isEmpty(_id)) {
                String pid = jdbcDao.phoneCount(params);
                //如果查询到电话号码存在，且不是当前用户的，则提示电话号码已被使用
                if (StringUtil.isNotEmpty(pid) && !_id.equals(pid)) {
                    return new AjaxJson(0, "电话号码已被注册，请重新输入");
                }
                _id = UUIDUtil.getInstance().getUUID();
                params.put("_id", _id);
                params.put("id", _id);
                params.put("password", "DD6E5E5918E94D997C686FCEBC56922F");
                f = insertMap(params);
            } else {
                params.put("id", _id);
                f = updateMap(params);
                fileUserDao.deleteFileUser(params);
            }
            String group = getParamsString(params, "users");
            String creator = getCreator(params);
            List<TransferData> lists = JSONArray.parseArray(group, TransferData.class);
            int c = 1;
            if (lists != null && !lists.isEmpty()) {
                for (TransferData transferData : lists) {
                    List<String> list = fileUserDao.queryFilesUserId(transferData.value);
                    c += list.size();
                    for (String id : list) {
                        GenFileUser genFileUser = new GenFileUser();
                        genFileUser.setId(UUIDUtil.getInstance().getUUID());
                        genFileUser.setGfId(id);
                        genFileUser.setGuId(_id);
                        genFileUser.setCreator(creator);
                        f += fileUserDao.insert(genFileUser);
                    }
                }
            }
            if (f == c) {
                return new AjaxJson(1, "保存成功");
            } else {
                throw new Exception("数据保存异常，请稍后再试！");
            }

        }
        return new AjaxJson("异常错误，请稍后再试");
    }
    /**
     * 重置
     *
     * @param params
     * @return
     */
    public AjaxJson reset(Map<String, Object> params) {
        int f = 0;
        if (params != null) {
            params.put("password", "DD6E5E5918E94D997C686FCEBC56922F");
            f = jdbcDao.resetPassword(params);
        }
        return new AjaxJson(f, f > 0 ? "密码重置成功" : "密码重置失败");
    }
}
